package vn.com.bookstore;

import java.io.File;
import java.io.IOException;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.http.HttpResponseCache;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

public class BookStoreApplication extends Application {
	public static String TAG = "BookStoreApplication";

	public static final String KEY_TEXT_SIZE = "text_size";

	public static final String KEY_PAGE_ID = "page_id";
	public static final String KEY_LAST_Y = "last_scroll_y";
	public static final String KEY_COLOR_LAYOUT = "layout_color";
	public static final String KEY_COLOR_TEXT = "text_color";
	public static final String KEY_FIRST_RUN = "first_run";

	private static SharedPreferences prefs;

	@Override
	public void onCreate() {
		super.onCreate();
		enableHttpCaching();

		prefs = this.getSharedPreferences("vn.com.bookstore", Context.MODE_PRIVATE);
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	public long getValue(String key, long defaultValue) {
		return prefs.getLong(key, defaultValue);
	}

	public int getValue(String key, int defaultValue) {
		return prefs.getInt(key, defaultValue);
	}

	public String getValue(String key, String defaultValue) {
		return prefs.getString(key, defaultValue);
	}

	public float getValue(String key, float defaultValue) {
		return prefs.getFloat(key, defaultValue);
	}

	public void setValue(String key, long value) {
		prefs.edit().putLong(key, value).commit();
	}

	public void setValue(String key, int value) {
		prefs.edit().putInt(key, value).commit();
	}

	public void setValue(String key, String value) {
		prefs.edit().putString(key, value).commit();
	}

	public void setValue(String key, float value) {
		prefs.edit().putFloat(key, value).commit();
	}
	
	public boolean getValue(String key, boolean defaultValue) {
		return prefs.getBoolean(key, defaultValue);
	}

	public void setValue(String key, boolean value) {
		prefs.edit().putBoolean(key, value).commit();
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private void enableHttpCaching() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			try {
				File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
				long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
				HttpResponseCache.install(httpCacheDir, httpCacheSize);
			} catch (IOException e) {
				Log.i(TAG, "OVER ICS: HTTP response cache failed:" + e);
			}
		} else {
			File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
			try {
				HttpResponseCache.install(httpCacheDir, 10 * 1024 * 1024);
			} catch (IOException e) {
				Log.i(TAG, "UNDER ICS : HTTP response cache  failed:" + e);
			}
		}
	}

	public void showToast(Context context, String message) {
		Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
		toast.cancel();
		toast.setText(message);
		toast.show();
	}

	public static boolean isTablet(Context context) {
		return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
	}
}
